package cn.pzhu.javaee.webapp.utils;
/**
 * 后端分页器
 */
import java.util.List;

import cn.pzhu.javaee.webapp.context.WebAppConstant;

public class Page<E> implements java.io.Serializable{
	private static final long serialVersionUID = -1277051852327956468L;
	private long total; //记录总数
	private int pageSize; //每页记录数	
	private int currentIndex=0;//当前页码
	private List<E> items;
	public Page(long total) {
		this(total,WebAppConstant.DEFAULT_PAGE_SIZE,1);
	}
	public Page(long total, int currentIndex) {
		this(total,WebAppConstant.DEFAULT_PAGE_SIZE,currentIndex);
	}
	public Page(long total, int pageSize, int currentIndex) {
		super();
		this.total = total;
		this.pageSize = pageSize;
		this.currentIndex = currentIndex;
		
	}
	/**
	 * 页数
	 * @return
	 */
	
	public int getPageCount(){		
        int div = (int) (total / pageSize);
        int result = (total % pageSize == 0) ? div : div + 1;        
        return result;
	}
	public void first(){
		currentIndex=1;
	}
	public void last(){
		currentIndex=getPageCount();
	}
	public int startIndex(){
		int startIndex=0;
		if(currentIndex>1){
			startIndex=(currentIndex-1)*pageSize;
		}
		return startIndex;
	}
	public boolean hasPre(){
		return 1>=currentIndex;
	}
	public boolean hasNext(){
		return getPageCount()<=currentIndex;
	}
	public long getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentIndex() {
		return currentIndex;
	}
	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}
	public List<E> getItems() {
		return items;
	}
	public void setItems(List<E> items) {
		this.items = items;
	}
	@Override
	public String toString() {
		return "Page [total=" + total + ", pageSize=" + pageSize
				+ ", currentIndex=" + currentIndex + "]";
	}
	
}
